import {Plugin, TransformResult,normalizePath} from "vite"

export default function vitePluginLogLevel(): Plugin {
  return {
    name: 'vite:vite-plugin-logLevel',
    transform(code: string, id: string): Promise<TransformResult> | TransformResult {
      let map = null
      // logWithPlugin.getFileName()  在代码中不判断换行
      code = code.replace(/logWithPlugin\.getFileName\(\)/g,($1,$0)=>{
        return `"${id.replace(normalizePath(process.cwd()),'')}"`
      })
      // logWithPlugin.getCurrentLine()
      code = code.split('\n').map((row, index) => {
        return row.replace(/logWithPlugin\.getCurrentLine\(\)/g, (index + 1) + '')
      }).join('\n')
      return {
        code,
        map
      }
    }
  }
}
